import java.util.*;

class  TreeNode{
    public  char val;
    TreeNode left;
    TreeNode right;

    public TreeNode(char val) {
        this.val = val;
    }
}
public class Order {
    //层序遍历
    void levelOrder(TreeNode root) {
        if(root==null){
            return;
        }
        Queue<TreeNode> queue=new LinkedList<>();
        queue.offer(root);
        TreeNode cur=null;
        while (!queue.isEmpty()){
            cur=queue.poll();
            System.out.print(cur.val+" ");
            if(cur.left!=null){
                queue.offer(cur.left);
            }
            if (cur.right!=null){
                queue.offer(cur.right);
            }

        }
    }
    public List<List<Character>> inOrder(TreeNode root) {
        List<List<Character>> list=new LinkedList<>();

        Queue<TreeNode> queue=new LinkedList<>();
        if(root==null){
            return null;
        }
        queue.offer(root);
        while (!queue.isEmpty()){
            int size=queue.size();
            List<Character> tmp=new ArrayList<>();
            while (size!=0){
                TreeNode cur=queue.poll();
                size--;
                tmp.add(cur.val);
                if(cur.left!=null){
                    queue.offer(cur.left);
                }
                if (cur.right!=null){
                    queue.offer(cur.right);
                }
            }
            list.add(tmp);
        }
        return list;


    }



    //中序遍历
    public void inorder(TreeNode root){
        if (root==null){
            return;
        }
        Stack<TreeNode> stack=new Stack<>();
        while(root!=null||!stack.isEmpty()){
            while (root!=null){
                stack.push(root);
                root=root.left;
            }
            TreeNode top=stack.pop();
            System.out.println(top.val);
            root=top.right;
        }


    }
}
